package com.cndw.kungfu.model.goods.app;

import java.util.Map;

import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.exception.ErrPopException;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.model.goods.PlayerBoxs;

/**
 * 背包仓库开格子
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-9-13 下午3:00:05
 */
public class AppDo105 extends AppBase {

	@Override
	public boolean check() throws Exception {
		if (goodsMode.getValueA()==1 && player.getBoxsGirds() >= ConstantsModel.playerGirdsMax) {
			throw new ErrPopException(LanguageLib.get("goods.use.boxmax"));
		}
		if (goodsMode.getValueA()==2 && player.getHoldGrids() >= ConstantsModel.playerGirdsMaxHold) {
			throw new ErrPopException(LanguageLib.get("goods.use.holdmax"));
		}	
		return true;
	}
	
	@Override
	public Map<String, Object> run() throws Exception {

		int openType = goodsMode.getValueA();
		int openNum = goodsMode.getValueB();

		PlayerBoxs playerBoxs = player.getPlayerBoxs();
		int boxsGirds = player.getBoxsGirds();
		int holdGrids = player.getHoldGrids();
		if (openType == 1) {
			boxsGirds += openNum;
			boxsGirds = boxsGirds >= ConstantsModel.playerGirdsMax ? ConstantsModel.playerGirdsMax : boxsGirds;
		} else if (openType == 2) {
			holdGrids += openNum;
			holdGrids = holdGrids >= ConstantsModel.playerGirdsMaxHold ? ConstantsModel.playerGirdsMaxHold : holdGrids;
		}

		playerBoxs.activationGirds(boxsGirds, holdGrids);

		player.setBoxsGirds(boxsGirds);
		player.setHoldGrids(holdGrids);
		player.commitSync();

		msg.put("playerBoxsGirds", boxsGirds);
		msg.put("playerHoldGirds", holdGrids);
		
		ticpString = LanguageLib.get("goods.use.suc");
		
		return msg;
	}

}
